package com.csdware.devilsalarm;

import static com.csdware.devilsalarm.Func_StaticMisc.copyFile;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.IOException;

public class Activity_RedemptionMiniGame extends AppCompatActivity {
    private Application_Data applicationData;
    private TextView DisplayNumber1;
    private TextView DisplayNumber2;
    private TextView ButtonGreater;
    private TextView ButtonLesser;
    private TextView ButtonEqual;
    private TextView DisplayMiniGameProgress;
    private TextView DisplayMiniGameHint;
    private int CurrentQuestionNumber = 0;
    private boolean IsLocked = false;
    private long TimestampGetLocked = 0;
    public final static int QuestionNumber = 300;  // 修改这个值设置总题数
    public final static int LockTimeS = 30;  // 错一个锁定30秒，以防用户通过守着大于号或小于号狂点来快速解锁

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        applicationData = (Application_Data) getApplicationContext();
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_redemption_minigame);
        DisplayNumber1 = findViewById(R.id.dsp_number1);
        DisplayNumber2 = findViewById(R.id.dsp_number2);
        ButtonGreater = findViewById(R.id.bt_greater);
        ButtonLesser = findViewById(R.id.bt_lesser);
        ButtonEqual = findViewById(R.id.bt_equal);
        DisplayMiniGameProgress = findViewById(R.id.dsp_mini_game_progress);
        DisplayMiniGameHint = findViewById(R.id.dsp_mini_game_hint);

        CheckIfQuestionAllCompleted();
        GenerateNewNumbers();

        {
            ButtonGreater.setOnClickListener(view -> {
                if (IsLocked) {
                    if (System.currentTimeMillis() - TimestampGetLocked > LockTimeS * 1000) {
                        IsLocked = false;
                    } else {
                        return;
                    }
                }
                int number1 = Integer.parseInt(DisplayNumber1.getText().toString());
                int number2 = Integer.parseInt(DisplayNumber2.getText().toString());
                if (number1 > number2) CurrentQuestionNumber += 1;
                else {
                    TimestampGetLocked = System.currentTimeMillis();
                    IsLocked = true;
                    Toast.makeText(getApplicationContext(), String.format(String.format(getString(R.string.wrong_try_again_d_seconds_later), LockTimeS)), Toast.LENGTH_SHORT).show();
                }
                CheckIfQuestionAllCompleted();
                GenerateNewNumbers();
            });
            ButtonEqual.setOnClickListener(view -> {
                if (IsLocked) {
                    if (System.currentTimeMillis() - TimestampGetLocked > LockTimeS * 1000) {
                        IsLocked = false;
                    } else {
                        return;
                    }
                }
                int number1 = Integer.parseInt(DisplayNumber1.getText().toString());
                int number2 = Integer.parseInt(DisplayNumber2.getText().toString());
                if (number1 == number2) CurrentQuestionNumber += 1;
                else {
                    TimestampGetLocked = System.currentTimeMillis();
                    IsLocked = true;
                    Toast.makeText(getApplicationContext(), String.format(String.format(getString(R.string.wrong_try_again_d_seconds_later), LockTimeS)), Toast.LENGTH_SHORT).show();
                }
                CheckIfQuestionAllCompleted();
                GenerateNewNumbers();
            });
            ButtonLesser.setOnClickListener(view -> {
                if (IsLocked) {
                    if (System.currentTimeMillis() - TimestampGetLocked > LockTimeS * 1000) {
                        IsLocked = false;
                    } else {
                        return;
                    }
                }
                int number1 = Integer.parseInt(DisplayNumber1.getText().toString());
                int number2 = Integer.parseInt(DisplayNumber2.getText().toString());
                if (number1 < number2) CurrentQuestionNumber += 1;
                else {
                    TimestampGetLocked = System.currentTimeMillis();
                    IsLocked = true;
                    Toast.makeText(getApplicationContext(), String.format(String.format(getString(R.string.wrong_try_again_d_seconds_later), LockTimeS)), Toast.LENGTH_SHORT).show();
                }
                CheckIfQuestionAllCompleted();
                GenerateNewNumbers();
            });
        }

        File sourceDirectory = new File(getFilesDir(), "ENCRYPTED_FILES");
        if (sourceDirectory.listFiles() != null)
            DisplayMiniGameHint.setText(String.format(getString(R.string.minigame_hint), sourceDirectory.listFiles().length));
    }

    private void GenerateNewNumbers() {
        int number1 = (int) (Math.random() * 100);
        int number2 = (int) (Math.random() * 100);
        DisplayNumber1.setText(String.valueOf(number1));
        DisplayNumber2.setText(String.valueOf(number2));
    }

    private void CheckIfQuestionAllCompleted() {
        DisplayMiniGameProgress.setText(CurrentQuestionNumber + "/" + QuestionNumber);
        if (CurrentQuestionNumber == QuestionNumber) {
            File sourceDirectory = new File(getFilesDir(), "ENCRYPTED_FILES");
            File destinationDirectory = new File(applicationData.HijackDirectory);
            File[] files = sourceDirectory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        String newFilename = file.getName();
                        if (file.getName().startsWith("###"))
                            newFilename = file.getName().substring(3, file.getName().length());
                        try {
                            copyFile(file, new File(destinationDirectory, newFilename));
                            file.delete();
                        } catch (IOException e) {
                            Log.e("Receiver_AlarmTimeUp", "Failed to copy file: " + file.getName(), e);
                        }
                    }
                }
            }
            Toast.makeText(getApplicationContext(), getString(R.string.congratulations_all_files_are_decrypted), Toast.LENGTH_SHORT).show();
        }
    }
}
